from src.dicom_viewer_be_mock_xiaosai.share import img_loader_dict

class ViewGroupManager:
    group_config = {}
    group_id_dict = {}  # group_id 对应的 sub_view_id
    sub_view_id_dict = {}  # sub_view_id 对应的 group_id、viewType

    @staticmethod
    def set_group_config(groupId, key, val):
        if groupId in ViewGroupManager.group_config:
            ViewGroupManager.group_config[groupId][key] = val
        else:
            ViewGroupManager.group_config[groupId] = {
                key: val
            }

    @staticmethod
    def parse_and_bind_ids(group_id, sub_view_ids):
        view_dict = {}
        for sub_view_id in sub_view_ids:
            _, _, _, view_type, _ = sub_view_id.split("__")
            view_dict[view_type] = sub_view_id
            ViewGroupManager.sub_view_id_dict[sub_view_id] = group_id
            ViewGroupManager.bind_img_loader(group_id, sub_view_id)
            if group_id not in ViewGroupManager.group_id_dict:
                ViewGroupManager.group_id_dict[group_id] = view_dict
                ViewGroupManager.group_config[group_id] = {
                    'invert': False
                }
            else:
                if view_type not in ViewGroupManager.group_id_dict[group_id]:
                    ViewGroupManager.group_id_dict[group_id][view_type] = sub_view_id

    @staticmethod
    def get_view_id_by_view_type(group_id, view_type):
        return ViewGroupManager.group_id_dict[group_id][view_type]

    @staticmethod
    def bind_img_loader(group_id, sub_view_id):
        img_loader = img_loader_dict[group_id]
        img_loader_dict[sub_view_id] = img_loader
